package com.msh.artascope.devcloud.service.api;

import com.msh.artascope.devcloud.client.po.EnvPo;
import com.msh.artascope.devcloud.client.qo.EnvQo;
import com.msh.artascope.devcloud.service.define.DevCloudDef;
import com.msh.artascope.devcloud.service.model.query.EnvQueryVo;
import com.msh.artascope.devcloud.service.model.vo.EnvCopyVo;
import com.msh.artascope.devcloud.service.model.vo.EnvVo;
import com.msh.artascope.devcloud.service.service.EnvService;
import com.msh.artascope.interfaces.IAccountGet;
import com.msh.artascope.interfaces.ILoginAccount;
import com.msh.frame.client.common.Result;
import com.msh.frame.client.exception.BizException;
import com.msh.frame.client.validation.IInsertVo;
import com.msh.frame.client.validation.IListVo;
import com.msh.frame.client.validation.IUpdateVo;
import com.msh.frame.web.base.BaseController;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import java.util.List;
import javax.validation.constraints.NotNull;
import javax.validation.groups.Default;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


/**
 * @author shihu
 * @mail m-sh@qq.com
 */
@Tag(name = "环境信息-api相关接口")
@Slf4j
@RestController
@RequestMapping("/api/env")
public class EnvApi extends BaseController<EnvPo, EnvQo> {

  @Autowired
  private EnvService envService;

  @Autowired
  private IAccountGet accountGet;


  @Operation(summary = "通用插入逻辑")
  @PostMapping("insert")
  public Result<Long> insert(@Validated({IInsertVo.class,Default.class}) @RequestBody EnvVo p) {
    ILoginAccount loginAccount = accountGet.getLoginAccount();
    BizException.doThrowIfNull("无法获取登录账号信息", loginAccount);
    p.initLoginAccount(loginAccount);
    return Result.successReturn(envService.insert(p));
  }

  @Operation(summary = "通用更新逻辑")
  @PostMapping("update")
  public Result update(@Validated({IUpdateVo.class,Default.class}) @RequestBody EnvVo p) {
    ILoginAccount loginAccount = accountGet.getLoginAccount();
    BizException.doThrowIfNull("无法获取登录账号信息", loginAccount);
    p.initLoginAccount(loginAccount);
    envService.update(p);
    return Result.successReturn();
  }


  @Operation(summary = "通用删除逻辑")
  @GetMapping("delete")
  public Result delete(@NotNull(message = "id不能为空") @RequestParam("id") Long id) {
    ILoginAccount loginAccount = accountGet.getLoginAccount();
    BizException.doThrowIfNull("无法获取登录账号信息", loginAccount);
    envService.delete(id, loginAccount.getTenantId());
    return Result.successReturn();
  }

  @Operation(summary = "获取详情")
  @GetMapping("get")
  public Result<EnvVo> get(@NotNull(message = "id不能为空") @RequestParam("id") Long id) {
    ILoginAccount loginAccount = accountGet.getLoginAccount();
    BizException.doThrowIfNull("无法获取登录账号信息", loginAccount);
    return Result.successReturn(envService.getVo(id, loginAccount.getTenantId()));
  }

  @Operation(summary = "vo列表查询")
  @PostMapping("list")
  public Result<List<EnvVo>> list(@Validated({IListVo.class}) @RequestBody EnvQueryVo q) {
    ILoginAccount loginAccount = accountGet.getLoginAccount();
    BizException.doThrowIfNull("无法获取登录账号信息", loginAccount);
    q.initLoginAccount(loginAccount);
    return Result.successReturn(envService.listVo(q));
  }


  @Operation(summary = "vo列表分页查询")
  @PostMapping("page")
  public Result<List<EnvVo>> page(@Validated({IListVo.class}) @RequestBody EnvQueryVo q) {
    ILoginAccount loginAccount = accountGet.getLoginAccount();
    BizException.doThrowIfNull("无法获取登录账号信息", loginAccount);
    q.initLoginAccount(loginAccount);
    return envService.pageVo(q);
  }

  @Operation(summary = "获取详情")
  @PostMapping("copyEnvConfig")
  public Result<EnvVo> copyEnvConfig(
      @Validated({DevCloudDef.Group1.class, Default.class}) @RequestBody EnvCopyVo p) {
    ILoginAccount loginAccount = accountGet.getLoginAccount();
    BizException.doThrowIfNull("无法获取登录账号信息", loginAccount);
    return Result.successReturn(
        envService.copyEnvConfig(p.getSourceEnvId(), p.getNewEnv(), p.getNewNamespace(),
            loginAccount.getTenantId()));
  }

  @Operation(summary = "获取详情")
  @PostMapping("copyProjectConfig")
  public Result<Void> copyProjectConfig(
      @Validated({DevCloudDef.Group2.class, Default.class}) @RequestBody EnvCopyVo p) {
    ILoginAccount loginAccount = accountGet.getLoginAccount();
    BizException.doThrowIfNull("无法获取登录账号信息", loginAccount);
    envService.copyProjectConfig(p.getSourceEnvId(), p.getTargetEnvId(), p.getProjectIds(),
        loginAccount.getTenantId());
    return Result.voidReturn();
  }

  @Operation(summary = "获取详情")
  @GetMapping("deleteNamespcae")
  public Result deleteNamespcae(@NotNull(message = "id不能为空") @RequestParam("id") Long id) {
    ILoginAccount loginAccount = accountGet.getLoginAccount();
    BizException.doThrowIfNull("无法获取登录账号信息", loginAccount);
    envService.deleteNamespace(id, loginAccount.getTenantId());
    return Result.successReturn();
  }

  @Operation(summary = "获取详情")
  @GetMapping("deleteProjectInfo")
  public Result deleteProjectInfo(@NotNull(message = "id不能为空") @RequestParam("id") Long id) {
    ILoginAccount loginAccount = accountGet.getLoginAccount();
    BizException.doThrowIfNull("无法获取登录账号信息", loginAccount);
    envService.deleteProjectInfo(id, loginAccount.getTenantId());
    return Result.successReturn();
  }


  @Operation(summary = "初始化命名空间")
  @PostMapping("initNamespace")
  public Result rebuildNamespace(@RequestBody EnvVo p){
    ILoginAccount loginAccount = accountGet.getLoginAccount();
    BizException.doThrowIfNull("无法获取登录账号信息", loginAccount);
    envService.initNamespace(p.getId(), loginAccount.getTenantId());
    return Result.successReturn();
  }
}
